class RuleEngine:
    def __init__(self):
        self.rules = self._load_rules()

    def _load_rules(self):
        return [
            {
                "name": "product_query",
                "conditions": [
                    {"or": ["价格", "多少钱", "售价", "价位", "成分", "功效", "用法", "适合", "肤质", "系列", "水",
                            "乳液", "面膜", "面霜", "精华", "洗面奶", "卸妆", "凝露", "推荐", "有没有", "哪款"]},
                    {"not": ["订单", "退货", "换货", "退款", "售后"]}
                ],
                "priority": 4
            },
            {
                "name": "order_query",
                "conditions": [
                    {"or": ["订单", "物流", "快递", "发货", "配送", "地址", "修改", "进度", "到哪里", "查询",
                            "什么时候"]},
                    {"not": ["退货", "换货", "售后", "退款"]}
                ],
                "priority": 3
            },
            # 添加更多具体规则
            {
                "name": "product_ingredients",
                "conditions": [
                    {"or": ["成分", "配料", "配方", "包含什么", "有哪些成分"]}
                ],
                "priority": 2
            },
            {
                "name": "product_effect",
                "conditions": [
                    {"or": ["功效", "效果", "作用", "功能", "能改善", "能保湿"]}
                ],
                "priority": 2
            },
            {
                "name": "product_usage",
                "conditions": [
                    {"or": ["用法", "使用", "操作", "步骤", "怎么用", "用几次", "哪一步"]}
                ],
                "priority": 2
            },
            {
                "name": "address_query",
                "conditions": [
                    {"or": ["地址", "收货", "配送", "修改地址", "更新地址", "改地址"]}
                ],
                "priority": 4
            }
        ]

    def apply_rules(self, text, entities):
        """应用规则引擎"""
        matched_intents = []

        for rule in self.rules:
            matched = True
            for condition in rule["conditions"]:
                if "or" in condition:
                    if not any(keyword in text for keyword in condition["or"]):
                        matched = False
                        break
                elif "and" in condition:
                    if not all(keyword in text for keyword in condition["and"]):
                        matched = False
                        break

            if matched:
                matched_intents.append({
                    "intent": rule["name"],
                    "confidence": 0.9,  # 规则匹配置信度高
                    "priority": rule["priority"]
                })

        # 按优先级排序
        matched_intents.sort(key=lambda x: x["priority"], reverse=True)
        return matched_intents